var outId =getQueryString("outId");
var backCode =getQueryString("backCode");
var relNum =getQueryString("relNum");
$(function () {
    $("#jqGrid").Grid({
        url: '../toolspartback/list?backCode=',
        colModel: [
            {label: 'id', name: 'id', index: 'ID', key: true, hidden: true},
            {label: '出库单号', name: 'outCode', index: 'OUT_CODE', width: 80},
            {
                label: '退库日期', name: 'backDate', index: 'BACK_DATE', width: 80, formatter: function (value) {
                    return transDate(value, 'yyyy-MM-dd')
                }
            },
            {label: '所属工装', name: 'partName', index: 'PART_ID', width: 80},
            {label: '货位', name: 'cargoLocation', index: 'CARGO_LOCATION', width: 80},
            {label: '名称', name: 'name', index: 'NAME', width: 80},
            {label: '单位', name: 'partUnit', index: 'PART_UNIT', width: 80},
            {label: '规格', name: 'spec', index: 'SPEC', width: 80},
            {label: '退库数量', name: 'backNum', index: 'BACK_NUM', width: 80},
           /* {label: '审核人', name: 'approvalUser', index: 'APPROVAL_USER', width: 80},
            {
                label: '审核时间', name: 'approvalTime', index: 'APPROVAL_TIME', width: 80, formatter: function (value) {
                    return transDate(value, 'yyyy-MM-dd')
                }
            },
            {
                label: '审核状态',
                name: 'approvalStstus',
                index: 'APPROVAL_STSTUS',
                width: 80,
                formatter: function (value) {
                    return sysEnums.CHECK_FLAG[value]
                }
            },*/
            {label: '操作人', name: 'createUser', index: 'CREATE_USER', width: 80},

            /* {label: '删除标识', name: 'isDel', index: 'IS_DEL', width: 80},
             {label: '备注', name: 'remark', index: 'REMARK', width: 80},
             {label: '物料编码', name: 'partCode', index: 'PART_CODE', width: 80},
             {label: '出库仓库ID', name: 'outStoreId', index: 'OUT_STORE_ID', width: 80},
             {label: '出库仓库', name: 'outStore', index: 'OUT_STORE', width: 80},
             {label: '退库单号', name: 'backCode', index: 'BACK_CODE', width: 80},
             {label: '修改时间', name: 'updateDate', index: 'UPDATE_DATE', width: 80},
             {label: '修改人员', name: 'updateUser', index: 'UPDATE_USER', width: 80},
             {label: '添加时间', name: 'createDate', index: 'CREATE_DATE', width: 80}*/]
    });
});

var vm = new Vue({
    el: '#rrapp',
    data: {
        showList: true,
        title: null,
        toolsPartBack: {
            isDel: '',
            remark: '',
            backNum: '',
            partUnit: '',
            spec: '',
            name: '',
            partCode: '',
            outStoreId: '',
            outStore: '',
            backDate: '',
            outCode: '',
            id: '',
            approvalStstus: '',
            approvalTime: '',
            approvalUser: '',
            cargoLocation: '',
            backCode: '',
            partId: '',
            updateDate: '',
            updateUser: '',
            createUser: '',
            createDate: '',
        },
        ruleValidate: {
            name: [
                {required: true, message: '名称不能为空', trigger: 'blur'}
            ],
            backNum: [
                {required: true, message: '退库数量不能为空', trigger: 'blur'}
            ]
        },
        q: {
            name: ''
        },
        outNum:0,//出库数量
     //   relNum:number,//实际出库数量
        num:0,
        partName:'',
    },
    methods: {
        query: function () {
            vm.reload();
        },
        add: function () {
            if(vm.relNum==0){
                alert('已全部退库,不能重复操作!');
                return
            }
            vm.showList = false;
            vm.title = "新增";
            vm.toolsPartBack = {
                isDel: '1',
                remark: '',
                backNum: '',
                partUnit: '',
                spec: '',
                name: '',
                partCode: '',
                outStoreId: '',
                outStore: '',
                backDate: '',
                outCode: '',
                id: '',
                approvalStstus: '',
                approvalTime: '',
                approvalUser: '',
                cargoLocation: '',
                backCode: '',
                partId: '',
                updateDate: '',
                updateUser: '',
                createUser: '',
                createDate: '',
            };
            var date = new Date();
            vm.toolsPartBack.backCode = 'back-' + date.getFullYear() + (date.getMonth() + 1) + (date.getDate()) + date.getHours() + date.getMinutes() + date.getSeconds()
            vm.getToolsSpartBack(outId)
        },
        update: function (event) {
            var id = getSelectedRow("#jqGrid");
            if (id == null) {
                return;
            }
            vm.showList = false;
            vm.title = "修改";

            vm.getInfo(id);
        },
        saveOrUpdate: function (event) {
            if(vm.relNum==0){
                alert('已全部退库,不能重复操作!');
                return
            }
            /**
             * 1.判断实际出库数量
             */
            if(vm.relNum>0){
                //实际出库与退库计算
                vm.num=vm.relNum-vm.toolsPartBack.backNum
            }else{
                //出库数量与退库计算
                vm.num=vm.outNum-vm.toolsPartBack.backNum
            }
            if(vm.num<0){
                alert('退库数量不能大于实际出库数量'+vm.outNum);
                vm.toolsPartBack.backNum=''
                return
            }
            var url = vm.toolsPartBack.id == (null || '') ? "../toolspartback/save" : "../toolspartback/update";
            Ajax.request({
                url: url,
                params: JSON.stringify(vm.toolsPartBack),
                type: "POST",
                contentType: "application/json",
                successCallback: function (r) {
                    alert('操作成功', function (index) {
                        vm.reload();
                    });
                }
            });
        },
        del: function (event) {
            var ids = getSelectedRows("#jqGrid");
            if (ids == null) {
                return;
            }

            confirm('确定要删除选中的记录？', function () {
                Ajax.request({
                    url: "../toolspartback/delete",
                    params: JSON.stringify(ids),
                    type: "POST",
                    contentType: "application/json",
                    successCallback: function () {
                        alert('操作成功', function (index) {
                            vm.reload();
                        });
                    }
                });
            });
        },
        getInfo: function (id) {
            Ajax.request({
                url: "../toolspartback/info/" + id,
                async: true,
                successCallback: function (r) {
                    vm.toolsPartBack = r.toolsPartBack;
                }
            });
        },
        reload: function (event) {
            vm.showList = true;
            var page = $("#jqGrid").jqGrid('getGridParam', 'page');
            $("#jqGrid").jqGrid('setGridParam', {
                postData: {'name': vm.q.name},
                page: page
            }).trigger("reloadGrid");
            vm.handleReset('formValidate');
        },
        reloadSearch: function () {
            vm.q = {
                name: ''
            };
            vm.reload();
        },
        handleSubmit: function (name) {
            handleSubmitValidate(this, name, function () {
                vm.saveOrUpdate()
            });
        },
        handleReset: function (name) {
            handleResetForm(this, name);
        },
        review: function () {
            vm.toolsPartBack.approvalStstus=1
            vm.getToolsSpartBack(outId)
            var rows = getSelectedRowData("#jqGrid");
            var ids = rows.id;
            if (ids == null) {
                return;
            }
            /**
             * 审核通过修改出库表中的--实际出库数量
             */
            if (rows.approvalStstus == "已审核") {
                alert('审核已通过,请勿重复审核!', function (index) {
                    vm.reload();
                });
                return;
            }
            vm.getInfo(ids);
            openWindow({
                title: "审核",
                area: ['350px', '300px'],
                content: jQuery("#reviewLayer"),
                btn: ['确定', '取消'],
                btn1: function (index) {
                    Ajax.request({
                        url:'../toolspartback/review',
                        params: JSON.stringify(vm.toolsPartBack),
                        type: 'POST',
                        contentType: "application/json",
                        successCallback: function (result) {
                            setTimeout(function () {
                                location.reload();
                            }, 1000);
                        }
                    });
                }
            });
        },
        getToolsSpartBack:function (id){
            Ajax.request({
                url: "../toolspartout/info/" + id,
                async: true,
                successCallback: function (r) {
                    vm.toolsPartOut=r.toolsPartOut
                    vm.toolsPartBack.outCode = r.toolsPartOut.outCode//出库单号
                    vm.toolsPartBack.name = r.toolsPartOut.name//名称
                    vm.toolsPartBack.partId = r.toolsPartOut.partId//所属工装
                    vm.partName = r.toolsPartOut.partName//所属工装
                    vm.toolsPartBack.cargoLocation = r.toolsPartOut.cargoLocation//货位
                    vm.toolsPartBack.spec = r.toolsPartOut.spec//规格
                    vm.outNum=r.toolsPartOut.outNum//出库数量
                    vm.relNum=r.toolsPartOut.relNum//实际出库
                }
            });
        }
    }
});
